// pages/receive/invalidDetails/invalidDetails.js
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canEdit: false,
    activeTab: 0,
    basicData: '', //基本信息
    invoiceList: [], // 发票
    taskList: [], // 任务号
    contractList: [], // 合同
    flowList: [], // 审批流程
    claimId: '', // 认领ID
    flowId: '', // 流程ID
    isInvalid: false, // 作废
    abolishFlowList: [], //作废审批流程
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    //认领ID
    this.setData({
      abolishId: options.abolishId,
      claimId: options.claimId
    })
    this.getData()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getData() {
    this.getBasicData()
    // this.getTaskList()
    // this.getinvoiceList()
    // this.getContractList()
  },
  onChange(e) {
    console.log(e)
    if (e.detail.index == 1) {
      this.getAbolishFlow()
    }
  },
  // 基本信息
  getBasicData() {
    let that = this;
    api.getClaimAbolishDataById({
      data: {
        abolishId: that.data.abolishId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          let now = new Date(res.data.data[0].claimAbolishDate);
          res.data.data[0].claimAbolishDate = util.formatTime(now).split(' ')[0];
          res.data.data[0].receiveMoney = util.formatFigure(res.data.data[0].receiveMoney)
          res.data.data[0].unclaimedMoney = util.formatFigure(res.data.data[0].unclaimedMoney)
          res.data.data[0].invoiceMoney = util.formatFigure(res.data.data[0].invoiceMoney)
          res.data.data[0].claimMoney = util.formatFigure(res.data.data[0].claimMoney)
          res.data.data[0].payableMoney = util.formatFigure(res.data.data[0].payableMoney)
          that.setData({
            basicData: res.data.data[0],
            flowId: res.data.data[0].flowId,
            radioBill: res.data.data[0].needInvoice ? '1' : '0',
            radioContract: res.data.data[0].needContract ? '1' : '0'
          })
          that.selectComponent('#tabs').resize();
        }
      }
    })
  },
  // 发票
  getinvoiceList() {
    let that = this;
    api.getClaimInvoiceByClaimId({
      data: {
        claimId: that.data.claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            res.data.data[i].money = util.formatFigure(res.data.data[i].money)
          }
          that.setData({
            invoiceList: res.data.data
          })
        }
      }
    })
  },
  // 任务号
  getTaskList() {
    let that = this;
    api.getClaimTaskByClaimId({
      data: {
        claimId: that.data.claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            res.data.data[i].claimMoney = util.formatFigure(res.data.data[i].claimMoney)
          }
          that.setData({
            taskList: res.data.data
          })
        }
      }
    })
  },
  // 合同
  getContractList() {
    let that = this;
    api.getClaimContractByClaimId({
      data: {
        claimId: that.data.claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            res.data.data[i].claimMoney = util.formatFigure(res.data.data[i].claimMoney)
          }
          that.setData({
            contractList: res.data.data
          })
        }
      }
    })
  },
  // 审批流程
  getFlow() {
    let that = this;
    api.getFlowByClaimId({
      data: {
        claimId: that.data.claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            let now = res.data.data[i].flowStepDate ? new Date(res.data.data[i].flowStepDate) : '';
            res.data.data[i].flowStepDate = res.data.data[i].flowStepDate ? util.formatTime(now) : '';
          }
          that.setData({
            flowList: res.data.data,
          })
        }
      }
    })
  },
  // 作废审批流程
  getAbolishFlow() {
    let that = this;
    api.getClaimAbolishFlow({
      data: {
        abolishId: that.data.abolishId
      },
      success: function (res) {
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            let now = res.data.data[i].flowStepDate ? new Date(res.data.data[i].flowStepDate) : '';
            res.data.data[i].flowStepDate = res.data.data[i].flowStepDate ? util.formatTime(now) : '';
          }
          that.setData({
            abolishFlowList: res.data.data,
          })
        }
      }
    })
  },
  // 作废原因
  onResultChange(e) {
    console.log(e)
    this.data.basicData.abolishReason = e.detail
  },
  // 备注
  onRemarksChange(e) {
    console.log(e)
    this.data.basicData.remarks = e.detail
  },
  // 删除
  onDel() {
    let that = this;
    Dialog.confirm({
      context: this,
      selector: "#van-dialog",
      message: '确定删除吗？',
    }).then(() => {
      api.cancelClaimAbolish({
        data: {
          abolishId: that.data.abolishId
        },
        success: function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '删除成功',
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              })
            }, 100);
          } else {
            wx.showModal({
              content: res.data.message,
              showCancel: false,
              confirmColor: '#0041C4'
            })
          }
        }
      })
    }).catch(() => {});

  },
  // 保存
  onSave() {
    let that = this;
    api.saveAbolishClaim({
      data: {
        id: that.data.abolishId,
        claimId: that.data.claimId,
        reason: that.data.basicData.abolishReason,
        remarks: that.data.basicData.remarks
      },
      method: 'POST',
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
          })
          that.getBasicData()
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
    })
  },
  // 提交
  onSubmit() {
    let that = this;
    if (that.data.basicData.abolishReason) {
      if (that.data.basicData.flowStatus == 1) {
        api.submitAbolishClaim({
          data: {
            id: that.data.abolishId,
            claimId: that.data.claimId,
            reason: that.data.basicData.abolishReason,
            remarks: that.data.basicData.remarks
          },
          method: 'POST',
          success: function (res) {
            if (res.data.code == 200) {
              wx.showToast({
                title: '提交成功',
              })
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                })
              }, 100);
            } else {
              wx.showModal({
                content: res.data.message,
                showCancel: false,
                confirmColor: '#0041C4'
              })
            }
          }
        })
      } else {
        api.resubmitAbolishClaim({
          data: {
            id: that.data.abolishId,
            claimId: that.data.claimId,
            reason: that.data.basicData.abolishReason,
            remarks: that.data.basicData.remarks
          },
          method: 'POST',
          success: function (res) {
            if (res.data.code == 200) {
              wx.showToast({
                title: '提交成功',
              })
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1,
                })
              }, 100);
            } else {
              wx.showModal({
                content: res.data.message,
                showCancel: false,
                confirmColor: '#0041C4'
              })
            }
          }
        })
      }

    } else {
      wx.showModal({
        content: '请填写作废原因',
        showCancel: false,
        confirmColor: '#0041C4'
      })
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})